require("TSLib")
require("luaext")


local floor = math.floor
local abs = math.abs

local Tool = class("Tool")


function Tool:ctor(rotate)
	--	屏幕方向，0 - 竖屏，1 - Home 键在右边，2 - Home 键在左边
	rotate = rotate or 0
	init(rotate)
	
	self.screenX,self.screenY = self:getScreenSize()
end
function Tool:sleep(time)
	mSleep(time)
end
function Tool:touchDown(x, y)
	touchDown(x, y)
end
function Tool:touchUp(x, y)
	touchUp(x, y)
end
function Tool:touchMove(x,y)
	touchMove(x, y)
end

--点击
function Tool:click(x, y)
    self:touchDown(x, y)
    self:sleep(30)
    self:touchMove(x, y) -- 安卓部分机型单点不生效的情况下可以加上此句
    self:touchUp(x, y)
end

--返回屏幕大小
function Tool:getScreenSize()
	return getScreenSize()
end

--以 width, height 分辨率为基准坐标进行缩放  
function Tool:setScreenScale(flag, width, height)
	setScreenScale(flag, width, height)
end

--保持屏幕，保持图像
function Tool:keepScreen(flag)
	keepScreen(flag)
end

--取色
function Tool:getColor(x, y)
	return getColor(x, y)
end
--取色
function Tool:getColorRGB(x, y)
	return getColorRGB(x, y)
end
--模糊找色
function Tool:findColor(x, y, color, scale)
	local s = scale or 90
    s = floor(0xff*(100-s)*0.01)
    local r,g,b = floor(c/0x10000),floor(c%0x10000/0x100),floor(c%0x100)
    local rr,gg,bb = self:getColorRGB(x,y)
    if abs(r-rr)<s and abs(g-gg)<s and abs(b-bb)<s then
        return true
    end
	return false
end

--模糊找色
function Tool:findColors(array, scale)
	local s = scale or 80
    s = floor(0xff*(100-s)*0.01)
    self:keepScreen(true)
    for var = 1, #array do
        local lr,lg,lb = self:getColorRGB(array[var][1],array[var][2])
        local r = floor(array[var][3]/0x10000)
        local g = floor(array[var][3]%0x10000/0x100)
        local b = floor(array[var][3]%0x100)
        if abs(lr-r) > s or abs(lg-g) > s or abs(lb-b) > s then
            self:keepScreen(false)
            return false
        end
    end
    self:keepScreen(false)
    return true
end

--找到色后点击指定位置
function Tool:findColorsAndClick(tbColor, tbClick)
	if not self:findColors(tbColor) then 
		return 
	end
	for k, v in pairs(tbClick) do
		self:click(v[1], v[2])
		if v[3] and v[3]>0 then 
			self:sleep(v[3])
		end		
	end
	return true
end

--在区域找色
function Tool:findColorInRegion(color, region, scale)
	local region = region or {0,0,self.screenX,self.screenY}
	local s = scale or 80
	local x,y = findColorInRegionFuzzy(color, s, region[1], region[2], region[3], region[4])

	if x==-1 and y==-1 then 
		return false
	end
	return x,y
end

--多点找色
--在屏幕上以左上角坐标 (0,0) 和右下角坐标 (639, 959)确定的区域（即全屏）范围内寻找符合偏移范围和颜色的3个坐标点
function Tool:findColorsInRegion(tbColor, region, scale)
--	dialog(dump(tbColor)..dump(region))
	local strColor = ""
	local region = region or {0,0,self.screenX,self.screenY}
	local color1 = tbColor[1]
	for i=2, #tbColor do 		
		local dx = tbColor[i][1] - color1[1]
		local dy = tbColor[i][2] - color1[2]
		strColor = strColor..dx.."|"..dy.."|"..tbColor[i][3]
		if i~=#tbColor then 
			strColor = strColor..","
		end
	end
	local s = scale or 80
	local x, y = findMultiColorInRegionFuzzy(color1[3], strColor, s, region[1], region[2], region[3], region[4])
	if x==-1 and y==-1 then 
		return false
	end
	return x, y
end

function Tool:findMultiColorInRegion2(colors, region)
	local fx,fy
	for k, color in pairs(colors) do 
		fx,fy = findColorInRegionFuzzy(color[3], 95, region[1], region[2], region[3], region[4])
		if fx==-1 or fy==-1 then 
			return false
		end
	end
	return  fx,fy
end


function Tool:toast(data, time)
	local time = time or 1
	toast(tostring(data),time)
end

function Tool:dialog(data, time)
	local time = time or 0
	dialog(tostring(data),time)
end

--测试
local tool = Tool.new()
tbColor={
	{  148,  237, 0x27a2ed},
	{  150,  237, 0x26a1ec},
	{  152,  243, 0x11335c},
}
local region = {122, 275, 181, 330}
--local x, y = tool:findColorsInRegion(tbColor)
--dialog(x..","..y, 0)
--local x, y = tool:findColorInRegion(0x609b58)
--if x then 
--	tool:click(x,y)
--end

local tbClick={{146, 298, 1000}, {209,  307}}
--local res = tool:findColorsAndClick(tbColor, tbClick)
--tool:toast(res)

return Tool